package com.jbm.jbmsupplier.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TextInputLayout;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.jbm.core.iface.IAcross;
import com.jbm.jbmsupplier.R;
import com.jbm.jbmsupplier.net.NetIntent;
import com.jbm.jbmsupplier.utlis.ExitActivity;
import com.jbm.jbmsupplier.view.DialogLoading;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by 家帮忙 on 2017/4/11.
 */

public class AmendPwass extends AllActivityApp {
    @InjectView(R.id.teleft)
    TextView teleft;
    @InjectView(R.id.tetitle)
    TextView tetitle;
    @InjectView(R.id.ymm)
    EditText ymm;
    @InjectView(R.id.xmm)
    EditText xmm;
    @InjectView(R.id.zcmm)
    EditText zcmm;
    @InjectView(R.id.bu_login)
    Button buLogin;
    DialogLoading dialogloading;
    private final int XIUGIPWD = 0001;
    @InjectView(R.id.ymms)
    TextInputLayout ymms;
    @InjectView(R.id.xmms)
    TextInputLayout xmms;
    @InjectView(R.id.zcmms)
    TextInputLayout zcmms;

    @Override
    public int setContentView() {
        return R.layout.amendpws;
    }

    @Override
    public void initIalizeView() {
        ExitActivity.getInstance().addActivity(this);
        ButterKnife.inject(this);
        tetitle.setText("修改密码");
    }

    private void initpewe() {
        String pwd, pwds, pwdss;
        pwd = ymm.getText().toString();
        pwds = xmm.getText().toString();
        pwdss = zcmm.getText().toString();
        if (pwd.equals("") || pwds.equals("") || pwdss.equals("")) {
//            hint("密码不能为空");
            ymms.setError("密码不能为空");
            xmms.setError("密码不能为空");
            zcmms.setError("密码不能为空");
            return;
        } else if (!pwdss.equals(pwds)) {
//            hint("两次密码不一致");
            xmms.setError("两次密码不一致");
            zcmms.setError("两次密码不一致");
            return;
        } else if (pwds.length() < 6) {
//            hint("密码长度至少需要6位");
            xmms.setError("密码长度至少需要6位");
            return;
        } else {
            ymms.setErrorEnabled(false);
            xmms.setErrorEnabled(false);
            zcmms.setErrorEnabled(false);
            NetIntent netintent = new NetIntent(handler);
            netintent.setAment(pwds, pwd, XIUGIPWD);
            dialogloading = new DialogLoading(this, "加载中....", false);
            dialogloading.show();
        }
    }

    private final int EXITLOGIN = 0x004;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case XIUGIPWD:
                    IAcross pwdsx = (IAcross) msg.obj;
                    if (pwdsx.m_code == 200) {
                        setDialogding();
                        setExitUser("密码修改成功请退出后" + "\n" + "重新登陆", handler, EXITLOGIN);
                    } else if (pwdsx.m_info.equals("Not Connect")) {
                        hint(pwdsx.m_info);
                        setDialogding();
                    } else if (pwdsx.m_code == 500) {
                        hint(pwdsx.m_info);
                        setDialogding();
                    } else {
                        hint(pwdsx.m_info);
                        setDialogding();
                    }
                    break;
                case EXITLOGIN:
                    IAcross exit = (IAcross) msg.obj;
                    if (exit.m_code == 200) {
                        Log.e("main", "退出成功");
                    } else {
                    }
                    break;
            }
        }
    };


    public void setDialogding() {
        if (dialogloading != null) {
            dialogloading.dismiss();
        }
    }

    @OnClick({R.id.teleft, R.id.bu_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.teleft:
                finish();
                break;
            case R.id.bu_login:
                initpewe();
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.inject(this);
    }
}
